import {request} from "@/utils/service"
import {obj2param} from "@/utils/index";
import {getToken} from "@/utils/cache/cookies";

const fileReq = {
  baseUrl: '/file',
  getFileList(basePath) {
    return request({
      timeout: 0,
      url: this.baseUrl + "/getFileList",
      method: "post",
      showLoading: false,
      params: {
        basePath
      }
    })
  },
  getDirFileList(basePath) {
    return request({
      url: this.baseUrl + "/getDirFileList",
      method: "post",
      params: {
        basePath
      }
    })
  },
  getGetUrl(file) {
    file = file.split('/').map(i => {
      return encodeURIComponent(i)
    }).join('/')
    return import.meta.env.VITE_BASE_API + this.baseUrl + `/getFileRange${file}?` + obj2param({
      Authorization: `Bearer ${getToken()}`,
    })
  },
  getFileUrl(file) {
    file = file.split('/').map(i => {
      return encodeURIComponent(i)
    }).join('/')
    return import.meta.env.VITE_BASE_API + this.baseUrl + `/getFile${file}?` + obj2param({
      Authorization: `Bearer ${getToken()}`,
    })
  },
  getFileIconUrl(file) {
    file = file.split('/').map(i => {
      return encodeURIComponent(i)
    }).join('/')
    return import.meta.env.VITE_BASE_API + this.baseUrl + `/getFileIcon${file}?` + obj2param({
      Authorization: `Bearer ${getToken()}`,
    })
  },
  getFile(file) {
    return request({
      url: this.baseUrl + `/getFile${file}`,
      method: "GET",
      responseType: 'blob'
    })
  },
  getUploaderFileUrl() {
    return import.meta.env.VITE_BASE_API + this.baseUrl + '/uploaderFile'
  },
  hasFile(basePath) {
    return request({
      url: this.baseUrl + "/hasFile",
      method: "POST",
      params: {
        basePath,
      }
    })
  },
  uploaderFile(basePath, file, whetherTheFil, call) {
    const formData = new FormData()
    formData.append('basePath', basePath)
    formData.append('file', file)
    const uploadRun = () => {
      let oldLoaded = 0
      let secLoaded = 0
      let oldTimeNow = Date.now()
      return request({
        showLoading: false,
        url: this.baseUrl + "/uploaderFile",
        method: "POST",
        data: formData,
        timeout: 0,
        onUploadProgress: progressEvent => {
          // progressEvent.loaded  ==> 已上传的大小
          //  progressEvent.total ==>  文件大小
          const percentCompleted = (progressEvent.loaded /
            progressEvent.total * 100).toFixed(2) / 1
          // 进度条的数字
          if (call) {
            if (Date.now() - oldTimeNow >= 1000) {
              secLoaded = progressEvent.loaded - oldLoaded
            }
            const params = {
              total: progressEvent.total, // 文件大小
              loaded: progressEvent.loaded, // 已上传的大小
              oldLoaded, // 上一次记录上传了多少
              secLoaded, // 一秒钟上传了多少
              diffLoaded: progressEvent.loaded - oldLoaded, // 差值
            }
            call(percentCompleted, params)
            oldLoaded = progressEvent.loaded
          }
        }
      })
    }
    if (whetherTheFil == '1') {
      return uploadRun()
    } else {
      return this.hasFile(basePath).then(res => {
        if (res.data == '1') {
          return "上传完成"
        } else {
          return uploadRun()
        }
      })
    }
  },
  getFileRange(file) {
    return request({
      url: this.baseUrl + `/getFileRange${file}`,
      method: "get",
      responseType: 'blob',
    })
  },
  getTextFile(file) {
    return request({
      url: this.baseUrl + "/getTextFile",
      method: "post",
      params: {
        filePath: file
      }
    })
  },
  saveTextFile(file, content) {
    const formData = new FormData()
    formData.append('filePath', file)
    formData.append('content', content)
    return request({
      url: this.baseUrl + "/saveTextFile",
      method: "post",
      data: formData,
    })
  },
  deleteFileOrDir(filePaths = []) {
    return request({
      url: this.baseUrl + "/deleteFileOrDir",
      method: "post",
      data: filePaths
    })
  },
  removeFileOrDir(filePaths, targetPath) {
    return request({
      url: this.baseUrl + "/removeFileOrDir",
      method: "post",
      data: filePaths,
      params: {
        targetPath
      }
    })
  },
  copyFileOrDir(filePaths, targetPath) {
    return request({
      url: this.baseUrl + "/copyFileOrDir",
      method: "post",
      data: filePaths,
      params: {
        targetPath
      }
    })
  },
  shareFileOrDir(shareFiles) {
    return request({
      url: this.baseUrl + "/shareFileOrDir",
      method: "post",
      data: shareFiles
    })
  },
  shareFileList(data) {
    return request({
      url: this.baseUrl + "/shareFileList",
      method: "post",
      data: data,
      isSearchList: true,
    })
  },
  shareFileDelete(shareFileDeletes) {
    return request({
      url: this.baseUrl + "/shareFileDelete",
      method: "post",
      data: shareFileDeletes,
    })
  },
  shareFileUpdate(row) {
    return request({
      url: this.baseUrl + "/shareFileUpdate",
      method: "post",
      data: row,
    })
  },
  getFileInfo(basePath) {
    return request({
      timeout: 0,
      url: this.baseUrl + "/getFileInfo",
      method: "post",
      params: {
        basePath
      }
    })
  },
  createFile(basePath) {
    return request({
      url: this.baseUrl + "/createFile",
      method: "post",
      params: {
        basePath
      }
    })
  },
  createDir(basePath) {
    return request({
      url: this.baseUrl + "/createDir",
      method: "post",
      params: {
        basePath
      }
    })
  },
  downloadFileOrDir(basePath, filePaths) {
    return request({
      url: this.baseUrl + "/downloadFileOrDir",
      method: "post",
      data: filePaths,
      params: {
        basePath
      }
    })
  },
  getDownloadFileOrDirById(downLoadId) {
    return import.meta.env.VITE_BASE_API + this.baseUrl + '/downloadFileOrDirById?' + obj2param({
      downLoadId
    })
  },
  getDiskInfo() {
    return request({
      showLoading: false,
      url: this.baseUrl + "/getDiskInfo",
      method: "post",
    })
  },
  getShareFile(id) {
    return request({
      url: this.baseUrl + "/getShareFile",
      method: "post",
      params: {
        id,
      }
    })
  },
  shareFileDown(data) {
    return request({
      url: this.baseUrl + "/shareFileDown",
      method: "post",
      data,
    })
  },
  rename(selectPathElement, value) {
    return request({
      url: this.baseUrl + "/rename",
      method: "post",
      params: {
        filePath: selectPathElement,
        newName: value
      },
    })
  }
}

export default fileReq
